<?php
/**
 * 功能描述：	菜单管理
 * @datetime	2022-07-09
 * @version   	v2.3.0
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_MenuServe extends Model {

    /**
     * Notes: 菜单列表
     * User: ZHL
     * Date: 2022/7/9
     */
    function getClassList($navtype = 2, $resultFlag = 0, $fid = 0){
        $userLevel = getUserLevel();
        $result = array();
        $classM = new Admin_MenuModel();
        $list = $classM->getClassList($navtype);
        if(empty($list)){
            return $result;
        }
        $powerSer = new Admin_PowerServe();
        $powerInfo = $powerSer->getUserPowerInfo();
        $statusArr = BaseData::$statusArr2;
        $statusOnOff = BaseData::$statusOnOff;
        foreach($list as $k=>$v){
            $id = $v['id'];$setpath = $v['setpath'];
            if(in_array($id, $powerInfo['menu_ids']) || in_array($setpath, $powerInfo['menu_ids']) || $userLevel == 1 || $navtype == -1){
                $showtype = $v['showtype']; $mould = $v['mould_code'];
                $url = 'admin_essay/m/'.$mould.'/c/'.$id;
                if($v['navtype'] == '1' && $v['spaceflag'] == '2') { // 顶部菜单且未开启空间
                    $url = 'admin_menu_find/c/' . $id;
                }else if($v['navtype'] == '2' && $v['spaceflag'] == '2') {  // 左侧菜单且未开启空间
                    $url = '';
                }else if($v['spaceflag'] == '3'){ // 直接外链模式
                    $url = 'admin_menu_updategourl/id/' . $id;
                }else if($v['setflag'] == '2'){ // 二次开发管理模式
                    $url = $setpath.'/c/' . $id;
                }
                $result[$k] = $v;
                $result[$k]['seturl'] = $url ? SYS_URL_PATH($url) : 'javascript:;';
                $result[$k]['showtypetxt'] = $showtype == 1 ? '单页' : '列表';
                $result[$k]['statustxt'] = isset($statusArr[$v['status']]) ? $statusArr[$v['status']] : $v['status'];
                $result[$k]['homeflagtxt'] = isset($statusOnOff[$v['homeflag']]) ? $statusOnOff[$v['homeflag']] : $v['homeflag'];
            }
        }
        if(!empty($result)){
            if($resultFlag){
                $result = getTree($result);
            }else{
                $result = getNavLevel($result,intval($fid));
            }
        }
        return $result;
    }

    /**
     * Notes: 添加菜单
     * User: ZHL
     * Date: 2022/7/9
     */
    function addClass($param){
        if (!isset($param['menuname']) || !$param['menuname']) {
            return '菜单名称不能为空';
        }
        if (!isset($param['mould_code']) || !$param['mould_code']) {
            return '模型编码不能为空';
        }
        $intParam = array(
            'fid','pagelist','viewseq','status','tmpltype','navtype','showtype',
            'addfind','spaceflag','nofollow','findflag','setflag','sideflag'
        );
        foreach($intParam as $v){
            if(isset($param[$v])){
                $param[$v] = intval($param[$v]);
            }
        }
        unset($param['subFlag']);
        if(isset($param['c']) ){unset($param['c']);}
        $data = $param;
        $data['createdate'] = date('Y-m-d H:i:s');
        $result = $this->conn('mysql')->addData('sysmenu', $data);
        return $result ? 'Y' : '添加失败';
    }

    /**
     * Notes: 修改菜单
     * User: ZHL
     * Date: 2022/7/9
     * @param $param
     * @return string
     */
    function updateClass($param){
        if (!isset($param['id']) || !$param['id']) {
            return '没有找到你要编辑的菜单';
        }
        if (!isset($param['menuname']) || !$param['menuname']) {
            return '菜单名称不能为空';
        }
        if (!isset($param['mould_code']) || !$param['mould_code']) {
            return '模型编码不能为空';
        }

        $intParam = array(
            'fid','pagelist','viewseq','status','tmpltype','navtype','showtype',
            'addfind','spaceflag','nofollow','findflag','setflag','sideflag'
        );
        foreach($intParam as $v){
            if(isset($param[$v])){
                $param[$v] = intval($param[$v]);
            }
        }
        $where['id'] = $param['id'];
        unset($param['id']);
        unset($param['subFlag']);
        if(isset($param['c']) ){unset($param['c']);}

        $commModel = new Admin_CommonModel();
        $essayInfo = $commModel->getEssayInfo('sysmenu', $where);
        if(empty($essayInfo)){
            return '修改失败';
        }

        $data = $param;
        if(!empty($data)){
            foreach($data as $k=>$v){
                $data[$k] = str_replace(SYS_LINK,'', $v);
            }
        }
        $result = $this->conn('mysql')->updateData('sysmenu', $data, $where);

        // 更新文件附件日志
        $fieldSer = new Admin_FieldServe();
        $fields = $fieldSer->getOpenClassFieldsInfos($where['id'], '1', '1');
        $commSer = new C_CommonServe();
        $fileFields = $commSer->getParamFieldFileType($fields, $param);
        foreach($fileFields as $fieldName){
            if(strpos($param[$fieldName],'|') !== false){
                foreach(explode('|', $param[$fieldName]) as $fieldOne){
                    $commSer->updateFileLogStatus($fieldOne, 1);
                }
            }else {
                $commSer->updateFileLogStatus($param[$fieldName], 1);
            }
        }
        foreach($fileFields as $fieldName){
            if(isset($essayInfo[$fieldName]) && $essayInfo[$fieldName]){
                if(strpos($essayInfo[$fieldName],'|') !== false){
                    foreach(explode('|', $essayInfo[$fieldName]) as $fieldOne){
                        $commSer->updateFileLogStatus($fieldOne, 1);
                    }
                }else {
                    $commSer->updateFileLogStatus($essayInfo[$fieldName], 2);
                }
            }
        }

        return $result ? 'Y' : '修改失败';
    }

    /**
     * Notes:删除
     * User: ZhuHaili
     */
    public function del($mouldCode, $id){
        $id = intval($id);
        if(!$id){
            return '请指定ID';
        }
        $where['id'] = $id;
        $db = $this->conn('mysql');
        $info = $db->findData($mouldCode, $where);
        if(empty($info)){
            return '没有找到要删除的内容';
        }
        $result = $db->delData($mouldCode, $where);
        return $result ? 'Y' : '删除失败';
    }

    /**
     * Notes:模型排序
     * User: ZhuHaili
     */
    function seq($mouldCode, $param){
        if(!is_array($param)){
            return '排序失败';
        }
        foreach($param as $id=>$seq){
            $where['id'] = $id;
            $data['viewseq'] = intval($seq);
            $this->conn('mysql')->updateData($mouldCode, $data, $where);
        }
        return 'Y';
    }

    /**
     * Notes:获取单个菜单详情
     * User: ZhuHaili
     * Date: 2022/9/28
     */
    public function getMenuInfo($where){
        if(empty($where)){
            return array();
        }
        return $this->conn('mysql')->findData('sysmenu', $where);
    }
}